package com.example.job;

import com.example.common.Result;
import com.example.entity.Relation;
import com.example.entity.Salesperson;
import com.example.entity.Task;
import com.example.entity.email.Mail;
import com.example.rabbit.ProduceService;
import com.example.service.RelationService;
import com.example.service.SalespersonService;
import com.example.service.TaskService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.ExecutorService;

@Slf4j
@Component
public class RelationAutoJob {
    @Resource
    private RabbitTemplate rabbitTemplate;

    @Value("${spring.mail.from}")
    private String from;

    @Resource
    private JavaMailSender mailSender;

    @Resource
    private RelationService relationService;
    @Resource
    private ExecutorService executorService;

    @Resource
    private  TaskService taskService;
    @Resource
    private SalespersonService salespersonService;
    @Resource
    private ProduceService produceService;


    @XxlJob("relationAutoJob")
    public  void exec() throws Exception {
        // 初始化线程池


        // 查询联系表的所有数据
        List<Relation> relations = relationService.findAlls();


        // 过滤并处理满足条件的数据
        relations.stream()
                .filter(data -> ChronoUnit.DAYS.between(data.getUpdateTime(), LocalDateTime.now()) > 7)
                .forEach(data -> {
                    String salespersonId= data.getSalespersonId();
                    Salesperson salesperson = salespersonService.findSalespersonById(salespersonId);
                    // 生成任务并提交到线程池
                    executorService.execute(() -> {
                        // 更新联系表的状态
                        // 发送邮件通知
                        Mail mail=new Mail();
                        mail.setTo(salesperson.getMailAdress());
                        mail.setTitle("任务来临喽");
                        mail.setContent("您有新的任务需要完成");
                        sendMail(mail);

                        // 插入任务表
                        insertTask(data);
                    });
                });

        // 关闭线程池
        executorService.shutdown();
    }

    private  void sendMail(Mail mail) {
        // 使用 RabbitMQ 或 Kafka 发送异步邮件通知
        // 具体实现略
        log.info("发送邮件给:{}",mail.getTo());
        produceService.sendByAck(mail);
    }

    private void insertTask(Relation data) {
        log.info("插入:{}",data);
        // 插入任务表的逻辑
        // 具体实现略
        String intermediatorId=data.getIntermediatorId();
        String salespersonId=data.getSalespersonId();
        Task task=new Task();

        task.setIntermediatorId(intermediatorId);
        task.setSalespersonId(salespersonId);
        task.setRalationTime(null);
        task.setState(String.valueOf(0));
        task.setCreateTime(LocalDateTime.now());
        task.setUpdateTime(LocalDateTime.now());
        taskService.insertTask(task);
    }

}
